local yongyi = fk.CreateSkill{
    name = "ym2__yongyi",
    tags = {Skill.Limited}
}
Fk:loadTranslationTable{
    ["ym2__yongyi"] = "雍仪",
    [":ym2__yongyi"] = "限定技，轮次开始时，你可令一名角色选择一项：1.弃置你三张牌，其所有技能本轮失效；2.本轮其前九次发动技能后，你可以摸一张牌或移动其场上一张牌。",
    ["#ym2__yongyi-invoke"] = "是否发动 雍仪？令一名角色选择一项",
    ["#ym2__yongyi-choice"] = "%dest对你发动了 雍仪，请选择一项",
    ["#ym2__yongyi1"] = "技能失效",
    [":#ym2__yongyi1"] = "弃置其三张牌，你所有技能本轮失效。",
    ["#ym2__yongyi2"] = "对方增益",
    [":#ym2__yongyi2"] = "本轮你前九次发动技能时，其可以摸一张牌或移动你场上一张牌。",
    ["#ym2__yongyi-discard"] = "弃置 %dest 三张牌",
    ["@[yongyi]ym2__yongyi-round"] = "被雍仪",
    ["ym2__yongyi_draw"] = "摸一张牌",
    ["ym2__yongyi_move"] = "移动%dest场上一张牌",
    ["#ym2__yongyi-ask"] = "雍仪：选择要将 %src 场上的牌移动给的角色",

    ["$ym2__yongyi1"] = "光武息兵而成中兴，朕岂惜斩一逆臣以谢天下。",
    ["$ym2__yongyi2"] = "昔明德皇后衣不曳地，今敕公卿，凡逾第者，家财皆没。",
}
Fk:addQmlMark{
    name = "yongyi",
    how_to_show = function(_, value, player)
        local num = 9
        for _, skill in ipairs(player:getAllSkills()) do
            if skill:isPlayerSkill(player) and not skill.name:startsWith("#") then
                num = num - player:usedSkillTimes(skill.name,Player.HistoryRound)
            end
        end
        return tostring(math.max(num,0))
    end,
    qml_path = function(name, value, p)
        if Self:isBuddy(p) or not string.startsWith(name, "@[yongyi]$") then
            return "packages/utility/qml/PlayerBox"
        end
        return ""
    end,
}
yongyi:addEffect(fk.RoundStart,{
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(yongyi.name) and player:usedSkillTimes(yongyi.name,Player.HistoryGame) == 0
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local tos = room:askToChoosePlayers(player,{
            targets = room.alive_players,
            max_num = 1,
            min_num = 1,
            prompt = "#ym2__yongyi-invoke",
            skill_name = yongyi.name,
        })
        if #tos > 0 then
            event:setCostData(self,tos[1])
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self)
        local choices = {}
        if #player:getCardIds("he") >= 3 then
            table.insert(choices,"#ym2__yongyi1")
        end
        table.insert(choices,"#ym2__yongyi2")
        local choice = room:askToChoice(to,{
            choices = choices,
            skill_name = yongyi.name,
            prompt = "#ym2__yongyi-choice::"..player.id,
            detailed = true,
            all_choices = {"#ym2__yongyi1","#ym2__yongyi2"}
        })
        if choice == "#ym2__yongyi1" then
            --local skills = {}
            for _, skill in ipairs(to:getAllSkills()) do
                if skill:isPlayerSkill(to) and not skill.name:startsWith("#") then
                    room:invalidateSkill(to, skill.name, "-round")
                end
            end
            local cards = room:askToChooseCards(to,{
                target = player,
                max = 3,
                min = 3,
                flag = "he",
                skill_name = yongyi.name,
                prompt = "#ym2__yongyi-discard::"..player.id
            })
            room:throwCard(cards, yongyi.name, player, to)
        else
            local mark = to:getTableMark("@[yongyi]ym2__yongyi-round")
            table.insertIfNeed(mark,player.id)
            room:setPlayerMark(to,"@[yongyi]ym2__yongyi-round",mark)
        end
    end,
})
yongyi:addEffect(fk.AfterSkillEffect,{
    can_refresh = function (self, event, target, player, data)
        if target and target:hasSkill(data.skill.name) and data.skill.visible and type(target:getMark("@[yongyi]ym2__yongyi-round")) == "table" and table.contains(target:getMark("@[yongyi]ym2__yongyi-round"),player.id) then
            local num = 9
            for _, skill in ipairs(target:getAllSkills()) do
                if skill:isPlayerSkill(target) and not skill.name:startsWith("#") then
                    num = num - target:usedSkillTimes(skill.name,Player.HistoryRound)
                end
            end
            return num >= 0
        end
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local choices = {"ym2__yongyi_draw"}
        if table.find(player.room.alive_players, function(p)
            return target:canMoveCardsInBoardTo(p, nil)
        end) then
            table.insert(choices, "ym2__yongyi_move::"..target.id)
        end
        local choice = room:askToChoice(player,{
            choices = choices,
            skill_name = yongyi.name,
        })
        if choice == "ym2__yongyi_draw" then
            player:drawCards(1,yongyi.name)
        else
            local targets = table.map(table.filter(player.room.alive_players, function(p)
                return target:canMoveCardsInBoardTo(p, nil)
            end), Util.IdMapper)
            if #targets > 0 then
                local to = room:askToChoosePlayers(player,{
                    targets = targets,
                    max_num = 1,
                    min_num = 1,
                    prompt = "#ym2__yongyi-ask:" .. target.id,
                    skill_name = yongyi.name,
                    cancelable = false,
                })
                if #to > 0 then
                    room:askToMoveCardInBoard(player,{
                        target_one = target,
                        target_two = to[1],
                        skill_name = yongyi.name,
                        move_from = target,
                    })
                end
            end
        end
    end,
})
return yongyi